﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AudioDjStudio;

namespace IntelliTouchMix
{
    public partial class Samples : Form
    {
        // private short nPlayer;
        public Samples()
        {
            InitializeComponent();
        }

        private void PlayPrevItem(short nPlayer, ref ListView listViewPlaylist)
        {
            audioDjStudio1.PlayListExecPrev(nPlayer);
        }

        private void PlayNextItem(short nPlayer, ref ListView listViewPlaylist)
        {
            audioDjStudio1.PlayListExecNext(nPlayer);
        }
        private void AddFileToPlayList(short nPlayer, ref ListView listViewPlaylist)
        {
            openFileDialog1.Filter =
                "Supported Sounds (*.mp3;*.mp2;*.wav;*.ogg;*.aiff;*.wma;*.wmv;*.asx;*.asf;" +
                "*.m4a;*.mp4;*.flac;*.aac;*.ac3;*.wv;" +
                "*.au;*.aif;*.w64;*.voc;*.sf;*.paf;*.pvf;*.caf;*.svx ;" +
                "*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3;*.cda)|" +
                "*.mp3;*.mp2;*.wav;*.ogg;*.aiff;*.wma;*.wmv;*.asx;*.asf;" +
                "*.m4a;*.mp4;*.flac;*.aac;*.ac3;*.wv;" +
                "*.au;*.aif;*.w64;*.voc;*.sf;*.paf;*.pvf;*.caf;*.svx ;" +
                "*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3;*.cda|" +
                "MP3 and MP2 sounds (*.mp3;*.mp2)|*.mp3;*.mp2|" +
                "AAC and MP4 sounds (*.aac;*.mp4)|*.aac;*.mp4|" +
                "WAV sounds (*.wav)|*.wav|" +
                "OGG Vorbis sounds (*.ogg)|*.ogg|" +
                "AIFF sounds (*.aiff)|*.aiff|" +
                "Windows Media sounds (*.wma;*.wmv;*.asx;*.asf)|*.wma;*.wmv;*.asx;*.asf|" +
                "AC3 sounds (*.ac3)|*.ac3;|" +
                "ALAC sounds (*.m4a)|*.ac3;|" +
                "FLAC sounds (*.flac)|*.flac;|" +
                "WavPack sounds (*.wv)|*.wv;|" +
                "MOD music (*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3)|*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3|" +
                "CD tracks (*.cda)|*.cda|" +
                "All files (*.*)|*.*";
            openFileDialog1.InitialDirectory = audioDjStudio1.SoundsDirectory;
            DialogResult result = openFileDialog1.ShowDialog();
            if (result != DialogResult.OK)
                return;
            MessageBox.Show(openFileDialog1.FileName);
            ListView.SelectedIndexCollection indexes = listViewPlaylist.SelectedIndices;
            short nInsertPosition;
            if (indexes.Count > 0)
                nInsertPosition = (short)(indexes[indexes.Count - 1] + 1);
            else
                nInsertPosition = (short)listViewPlaylist.Items.Count;
            audioDjStudio1.PlayListAddItem(nPlayer, openFileDialog1.FileName, nInsertPosition);
            short nItemsCount = audioDjStudio1.PlayListGetCount(nPlayer);
            // update the listview contents
            FillPlayListView(nPlayer, ref listViewPlaylist);
        }

        private void FillPlayListView(short nPlayer, ref ListView listViewPlaylist)
        {
            // empty list starting from the last item
            int nCurrItems = listViewPlaylist.Items.Count;
            if (nCurrItems > 0)
            {
                for (int x = nCurrItems - 1; x >= 0; x--)
                    listViewPlaylist.Items.RemoveAt(x);
            }

            // fill list with playlist file contents
            short nItemsCount = audioDjStudio1.PlayListGetCount(nPlayer);

            for (short i = 0; i < nItemsCount; i++)
            {
                string strIndex = i.ToString();
               string strDuration = audioDjStudio1.PlayListGetItemString(nPlayer, i, enumPlayListStrings.PL_STRING_INFO_FORMATTED_DURAT_NO_MS);
                string strTitle = audioDjStudio1.PlayListGetItemString(nPlayer, i, enumPlayListStrings.PL_STRING_INFO_TITLE);
                if (strTitle.Length == 0)
                    strTitle = audioDjStudio1.PlayListGetItemString(nPlayer, i, enumPlayListStrings.PL_STRING_FILENAME);

                string[] listStrings = new string[] { strIndex, strDuration, strTitle };
                ListViewItem item = new ListViewItem(listStrings, -1);
                listView1.Items.Add(item);
            }
        }

        private void RemoveFileFromPlayList(short nPlayer, ref ListView listViewPlaylist)
        {
            ListView.SelectedIndexCollection indexes = listViewPlaylist.SelectedIndices;
            if (indexes.Count == 0)
                return;

            for (int i = (indexes.Count - 1); i >= 0; i--)
                audioDjStudio1.PlayListRemoveItem(nPlayer, (short)indexes[i]);

            // update the listview contents
            FillPlayListView(nPlayer, ref listViewPlaylist);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddFileToPlayList(Player_1, ref listView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RemoveFileFromPlayList(Player_1, ref listView1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SavePlayList(Player_1, ref listView1);
        }

        private void SavePlayList(short Player_1, ref ListView listView1)
        {
            saveFileDialog1.Filter = "M3U PlayList (*.m3u)|*.m3u|PLS PlayList (*.pls)|*.pls|WPL PlayList (*.wpl)|*.wpl|PDJ PlayList (*.pdj)|*.pdj";
            saveFileDialog1.InitialDirectory = audioDjStudio1.SoundsDirectory;
            saveFileDialog1.FilterIndex = 3;
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == DialogResult.Cancel)
                return;

            if (audioDjStudio1.PlayListSave(Player_1, saveFileDialog1.FileName, (enumPlayListFormats)(saveFileDialog1.FilterIndex - 1)) != enumErrorCodes.NOERROR)
            {
                MessageBox.Show("Error saving playlist");
                return;
            }
        }

        private void PlayItem(short nPlayer, ref ListView listView1)
        {
            ListView.SelectedIndexCollection indexes = listView1.SelectedIndices;
            if ((indexes.Count > 1) || (indexes.Count == 0))
            {
                audioDjStudio1.PlayListExecute(nPlayer, true);
                return;
            }

            short nCurSel = (short)indexes[0];
            audioDjStudio1.PlayListExecAt(nPlayer, nCurSel);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            PlayItem(Player_1, ref listView1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            audioDjStudio1.StopSound(Player_1);		
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.StreamVolumeSet(Player_1, (short) trackBar1.Value, enumVolumeScales.SCALE_LINEAR);
        }

        private void Samples_Load(object sender, EventArgs e)
        {
            audioDjStudio1.InitSoundSystem(1, 0, 0, 0, 0, -1);
            enumErrorCodes er= audioDjStudio1.PlayListCreate(Player_1);
            // trackBar1.Value = 100;
        }

     

       
        
    }
}
